package com.xcmg.restaurant.restuarantservice.config;

import org.springframework.cloud.loadbalancer.annotation.LoadBalancerClient;
import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.ThreadLocalRandom;

/**
 * LoadBalancerConfiguration
 *
 * @author diyiliu
 * @create 2024-07-18 15:29
 **/

@Configuration
@LoadBalancerClient(name = "customer-service", configuration = LoadBalancerConfiguration.class)
public class LoadBalancerConfiguration {

    @Bean
    public ServiceInstanceListSupplier discoveryClientServiceInstanceListSupplier(ConfigurableApplicationContext context) {
        System.out.println("Configuring Load balancer to prefer same instance");

        return ServiceInstanceListSupplier.builder()
                .withBlockingDiscoveryClient()
                .withSameInstancePreference()
                .build(context);
    }
}
